<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script>
        /*设置token为全局变量*/
        var token = '';
        function fun02() {
            var name = $("#name").val();
            var password = $("#password").val();
            $.ajax({
                url: "/login",
                type: "POST",
                contentType : "application/json",
                dataType: "json",
                data: JSON.stringify({
                    account: name,
                    userName: name,
                    password: password
                }),
                success: function (data) {
                    console.log(data);
                    $("#token").val("");
                    $("#token").val(data.data);
                    token = data.data
                    // alert(token)
                }
            })
        }

        function fun01() {
            $.ajax({
                url: "/admin/msg",
                type: "get",
                data: {},
                headers: {'Authorization': token},
                success: function (data) {
                    alert(data)
                }
            })
        }
    </script>
</head>
<body>
<form id="login">
    姓名:<input type="text" name="username" id="name" value="1"><br>
    密码:<input type="text" name="password" id="password" value="1"><br>
    <input type="button" value="登录" onclick="fun02()">
    <input id="token">
</form>
<br>
<input type="button" value="ajax请求" onclick="fun01()">
</body>
</html>